﻿using CNFM_CROCODILE_LOGO.Core.Domain.Current;
using CNFM_CROCODILE_LOGO.Core.Domain.Entity;
using CNFM_CROCODILE_LOGO.Core.Domain.ViewModels;
using CNFM_CROCODILE_LOGO.Core.Repository;
using CNFM_CROCODILE_LOGO.RestClient.Domain;
using CNFM_CROCODILE_LOGO.RestClient.Utils;
using CNFM_CROCODILE_LOGO.Services.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CNFM_CROCODILE_LOGO.Services.Implementations
{
    /// <summary>
    /// 标识接口实现
    /// </summary>
    public class SignageWXServices : ISignageWXServices
    {
        private IEfRepository _rep;

        public SignageWXServices(IEfRepository rep)
        {
            this._rep = rep;
        }

        /// <summary>
        /// 标识申请单列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="status"></param>
        /// <param name="sasearch"></param>
        /// <returns></returns>
        public PaginationModel GetSignageApplicationAll(int index, int size, string status, string sasearch)
        {
            var enterpriseId = _rep.FirstOrDefault<CrocodileUser>(p => p.Id == UserBasic.userId).EnterpriseId;
            var signageApplicationList = from a in _rep.Where<SignageApplicationReceipt>(p => true)
                                         join e in _rep.Where<Enterprise>(p => p.Status == 0 && p.Id == enterpriseId) on a.EnterpriseId equals e.Id
                                         join s in _rep.Where<SpeciesManage>(p => p.Status == 0) on a.SpeciesId equals s.Id
                                         join d in _rep.Where<Dictionary>(p => p.Status == 0) on s.Type equals d.Id
                                         join u in _rep.Where<CrocodileUser>(p => p.Status == 0) on a.CreateId equals u.Id
                                         select new
                                         {
                                             a.Id,
                                             a.Remarks,
                                             a.Status,
                                             a.CreateId,
                                             a.RegisteredTime,
                                             a.Person,
                                             a.EnterpriseId,
                                             a.ApplyNum,
                                             a.OneToExamineNum,
                                             a.TwoToExamineNum,
                                             a.ThreeToExamineNum,
                                             a.OneRemarks,
                                             a.TwoRemarks,
                                             a.SpeciesId,
                                             a.Details,
                                             a.Purpose,
                                             CreateName = u.UserName,
                                             e.EnterpriseName,
                                             s.SpeciesName
                                         };


            var signageApplicationAll = signageApplicationList.OrderByDescending(p => p.RegisteredTime)
                .WhereIf(p => p.EnterpriseName.Contains(sasearch) || p.SpeciesName.Contains(sasearch)
                || p.CreateName.Contains(sasearch) || p.Purpose.Contains(sasearch), string.IsNullOrEmpty(sasearch) == false)
                .WhereIf(p => p.Status == Convert.ToInt32(status), string.IsNullOrEmpty(status) == false);
            var total = signageApplicationAll.Count();

            var models = signageApplicationAll.ToPaginationList(index, size)
                .ToList();
            return new PaginationModel { Code = 200, Msg = "success", Total = total, Result = new { Data = models } };
        }
     
        /// <summary>
        /// 标识申请单详情
        /// </summary>
        /// <param name="signagId"></param>
        /// <returns></returns>
        public ReturnValueModel GetSignageApplication(string signagId)
        {
            if (string.IsNullOrEmpty(signagId))
                return new ReturnValueModel { Code = 400, Msg = "申请单Id,不能为空!" };

            var signageApplicationList = _rep.Where<SignageApplicationReceipt>(p => p.Status != -1 && p.Id == signagId).ToList();
            if (signageApplicationList.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到该详情,请确认是否为可用状态!" };
            var model = signageApplicationList.FirstOrDefault();

            return new ReturnValueModel { Code = 200, Msg = "success!", Result = new { Data = model } };
        }

        /// <summary>
        /// 标识申请单新增修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValueModel AddUpdateSignageApplication(SignageApplicationReceiptModels model)
        {
            List<string> mgsList = new List<string>();
            mgsList.Add(model.ApplyNum == 0 ? "申请数量" : null);
            mgsList.Add((string.IsNullOrEmpty(model.SpeciesId) ? "产品" : null));
            mgsList.Add((string.IsNullOrEmpty(model.Person) ? "申请人" : null));
            foreach (var item in mgsList)
                if (!string.IsNullOrEmpty(item))
                    return new ReturnValueModel { Code = 400, Msg = String.Format("您的{0}没有填写!", item) };

            var enpId = _rep.FirstOrDefault<CrocodileUser>(p => p.Id == UserBasic.UserId()).EnterpriseId;

            if (string.IsNullOrEmpty(model.Id))
            {
                SignageApplicationReceipt item = new SignageApplicationReceipt()
                {
                    Id = Guid.NewGuid().ToString(),
                    Status = 0,
                    CreateId = UserBasic.UserId(),
                    CreateTime = DateTime.Now,
                    Remarks = model.Remarks,
                    SignageApplicationNo = string.Format("{0}{1}", UtilsHelp.GetTimeStamp(), UtilsHelp.getRandNum(4)),
                    RegisteredTime = model.RegisteredTime,
                    Person = model.Person,
                    EnterpriseId = enpId,
                    ApplyNum = model.ApplyNum,
                    SpeciesId = model.SpeciesId,
                    Details = model.Details,
                    Purpose = model.Purpose
                };

                _rep.Insert(item);
                return new ReturnValueModel { Code = 200, Msg = "success!" };
            }

            var signageAplList = _rep.Where<SignageApplicationReceipt>(p => p.Status == 0 && p.Id == model.Id).ToList();
            if (signageAplList.Count() == 0)
                return new ReturnValueModel { Code = 404, Msg = "查找不到该申请单,请确认是否为可用状态!" };

            var signageApItem = signageAplList.FirstOrDefault();
            signageApItem.RegisteredTime = model.RegisteredTime;
            signageApItem.Person = model.Person;
            signageApItem.ApplyNum = model.ApplyNum;
            signageApItem.SpeciesId = model.SpeciesId;
            signageApItem.Purpose = model.Purpose;
            signageApItem.Details = string.IsNullOrEmpty(model.Details) ? signageApItem.Details : model.Details;
            signageApItem.Remarks = string.IsNullOrEmpty(model.Remarks) ? signageApItem.Remarks : model.Remarks;

            _rep.Update(signageApItem);
            return new ReturnValueModel { Code = 200, Msg = "success!" };
        }
    }
}
